package operate

import (
	"gin-demo/common"
	"gin-demo/container"
	"gin-demo/entity"
	"gorm.io/gorm"
)

type UserDaoImp struct {
}

func (c UserDaoImp) List(pageRequest common.PageRequest) (*entity.UserList, error) {

	offset, limit := common.PageHandle(pageRequest)
	var users []*entity.User
	var userDB = container.Get("db").(*gorm.DB)
	err := userDB.
		Table("user").
		Offset(offset).
		Limit(limit).
		Find(&users).Error
	var count int64
	err = userDB.
		Table("user").
		Count(&count).Error
	if err != nil {
		return nil, err
	}
	var pageResponse common.PageResponse
	pageResponse.Total = count
	pageResponse.PageInfo = pageRequest
	userList := &entity.UserList{
		PageResponse: pageResponse,
		Users:        users,
	}
	return userList, nil

}
